<template>
  <div>
    <!-- 头部搜索区域 -->
    <el-form inline :model="searchMap" ref="searchMap" class="demo-form-inline" style="margin-top:10px">
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="searchMap.code" placeholder="商品编号"></el-input>
      </el-form-item>
      <el-form-item prop="supplierName">
        <el-input readonly v-model="searchMap.supplierName" @click.native="choseSupplier" placeholder="选择供应商">
          {{searchMap.supplieName}}</el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="getData"></el-button>
        <el-button icon="el-icon-circle-plus-outline" type="primary" @click="addGoods"></el-button>
        <el-button icon="el-icon-refresh" type="primary" @click="resetForm('searchMap')"></el-button>
      </el-form-item>
    </el-form>
    <!-- 表单区域 -->
    <el-table :data="list" border style="width: 100%">
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column prop="code" label="商品编码"></el-table-column>
      <el-table-column prop="spec" label="商品规格"></el-table-column>
      <el-table-column prop="retailPrice" label="零售价"></el-table-column>
      <el-table-column prop="purchasePrice" label="进货价"></el-table-column>
      <el-table-column prop="storageNum" label="库存数量"></el-table-column>
      <el-table-column prop="supplierName" label="供应商"></el-table-column>

      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="editGoods(scope.row.id)"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteGoods(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total"></el-pagination>
    <!-- 弹出层 -->
    <el-dialog title="选择供应商" :visible.sync="addDialogVisible" width="500px">
      <supplier :isDialog="true" @option-supplier="optSupplier"></supplier>
    </el-dialog>

    <!-- 新增商品弹出框 -->
    <el-dialog :title="addList.id?'编辑商品':'新增商品'" :visible.sync="DialogVisible" @close="resetAdd" width="550px">
      <el-form :model="addList" ref="addList" label-width="100px" label-position="right" style="width:400px">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="addList.name"></el-input>
        </el-form-item>
        <el-form-item label="商品编码" prop="code">
          <el-input v-model="addList.code"></el-input>
        </el-form-item>
        <el-form-item label="商品规格" prop="spec">
          <el-input v-model="addList.spec"></el-input>
        </el-form-item>
        <el-form-item label="零售价" prop="retailPrice">
          <el-input v-model="addList.retailPrice"></el-input>
        </el-form-item>
        <el-form-item label="进货价" prop="purchasePrice">
          <el-input v-model="addList.purchasePrice"></el-input>
        </el-form-item>
        <el-form-item label="库存数量" prop="storageNum">
          <el-input v-model="addList.storageNum"></el-input>
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input readonly v-model="searchMap.supplierName" @click.native="choseSupplier" placeholder="选择供应商">
            {{searchMap.supplierName}}</el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="DialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addList.id?submitEdit(addList):submitAdd(addList)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Supplier from '@/views/supplier'
  import goodsApi from '@/api/goods'

  export default {
    components: {
      Supplier
    }, // 作为子组件
    data() {
      return {
        // 供应商搜索
        searchMap: {
          name: '',
          code: ''
        },
        // 表单内容
        list: [],
        // 总条数
        total: 0,
        // 当前页码
        currentPage: 1,
        // 每页显示10条数据,
        pageSize: 10,
        // 选择供应商弹出窗
        addDialogVisible: false,
        // 编辑/新增
        addList: {
          id:null,
          name: '',
          code: '',
          spec: '',
          retailPrice: '',
          purchasePrice: '',
          storageNum: '',
          supplierName: ''
        },
        DialogVisible: false


      }
    },
    created() {
      this.getData()
    },

    methods: {
      // 获取数据
      getData() {
        goodsApi.search(this.currentPage, this.pageSize, this.searchMap).then(res => {
          console.log(res.data.data);
          this.total = res.data.data.total
          this.list = res.data.data.rows
        })
      },
      // 新增
      addGoods() {
        this.DialogVisible = true
      },
      // // 重置表单
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.addList = {}
        this.searchMap = {}

      },
      resetAdd() {
        this.$refs.addList.resetFields();
        this.addList = {}
        this.searchMap = {}

      },
      // 选择供应商
      choseSupplier() {
        this.addDialogVisible = true
      },
      // 编辑用户
      editGoods(id) {
        this.DialogVisible = true
        goodsApi.getById(id).then(res => {
          this.addList = res.data.data
        })
      },
      // 提交编辑
      submitEdit(data) {
        goodsApi.update(data).then(res => {
          console.log('编辑', res.data);
          this.DialogVisible = false
        })

      },
      // 提交新增
      submitAdd(data) {
        this.DialogVisible = false
        goodsApi.add(data).then(res => {
          console.log('新增', res.data);
          this.getData()
        })

      },
      // 删除供应商
      deleteGoods(id) {
        console.log(id);
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          goodsApi.delete(id).then(res => {
            console.log(res.data);
          })
          this.getData()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      },
      // 分页功能
      handleSizeChange(val) {
        console.log("每页显示条数", val);
        this.pageSize = val;
        this.getData();
      },
      handleCurrentChange(val) {
        console.log("当前页码", val);
        this.currentPage = val;
        this.getData();
      },
      // 接收子组件传递过来的一行数据
      optSupplier(row) {
        console.log('父组件接收到了', row);
        this.searchMap.supplierName = row.name
        this.searchMap.supplierId = row.id
        this.addDialogVisible = false
      }
    }
  }
</script>

<style lang='less' scoped>
  .chooseBtn {
    width: 110px;
    height: 40px;
    color: #ccc;
    text-align: center;
  }
</style>